package com.szpx.controller.yang;

import com.szpx.entity.yang.YangDao;
import com.szpx.service.yang.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/api1")
public class ProductsController {

    private ProductsService productsService;

    @Autowired
    public ProductsController(ProductsService productsService) {
        this.productsService = productsService;
    }

    @GetMapping("/products")
    public ModelAndView getProducts() {
        List<YangDao> products = productsService.findAll();
        return new ModelAndView("products", "products", products);
    }

    @PostMapping("/products/update")
    public ModelAndView updateProduct(YangDao product) {
        productsService.updateProduct(product);
        return new ModelAndView("redirect:/api1/products");
    }

    @PostMapping("/products/add")
    public ModelAndView addProduct(YangDao product) {
        productsService.addProduct(product);
        return new ModelAndView("redirect:/api1/products");
    }

    @PostMapping("/products/delete")
    public ModelAndView deleteProduct(Long productId) {
        productsService.deleteProduct(productId);
        return new ModelAndView("redirect:/api1/products");
    }
}

